function sumFactors(n) {
    let sum = 1;
    for (let i = 2; i <= n / i; i++) {
        if (n % i === 0) {
            sum += i;

            if (i * i !== n) {
                sum += n / i;
            }
        }
    }
    return sum;
}

function sumAmicableNum(n) {
    let sum = 0;
    for (let i = 1; i <= n; i++) {
        let j = sumFactors(i);
        if (j === i) continue;
        if (sumFactors(j) === i) {
            sum += j;
        }
    }
    return sum;
}

console.log(sumAmicableNum(10000));